﻿#include "stdafx.h"
#include "XControlIntParser.h"

#include "XBSTR.h"
#include "XString.h"
#include "StrUtil.h"
#include "Util.h"
#include "IXColorDraw.h"
#include "IXImageDraw.h"

#include "ParseHelper.h"


CXControlIntParser::CXControlIntParser() : CXIntParserBase(&TIntCreateControl<IXControl>)
{
    ;
}

CXControlIntParser::~CXControlIntParser()
{
    ;
}

// 创建控件之前
BOOL CXControlIntParser::PreCreate(IN IXConfigNode* pNode, IN IXControl* pControl)
{
    return TRUE;
}

// 创建控件之前
BOOL CXControlIntParser::CreateUI(IN IXConfigNode* pNode, IN IXControl* pControl, IN IXControl* pParent)
{
    BOOL bAutoSize = FALSE;
    POINT Pt = {0};
    HRESULT hResult = pControl->Create(pParent, &Pt, NULL);

    return SUCCEEDED(hResult);
}

// 填充属性
BOOL CXControlIntParser::FillAttr(IN IXConfigNode* pNode, IN IXControl* pControl)
{
    RECT rcWnd = {0};
    XBSTR bstrTemp;
    SIZE sizeTemp;
    LONG lTemp;
    BOOL bTemp;

    // POS
    HRESULT hResult = pNode->GetValue(_T("pos"), &sizeTemp);
    if(SUCCEEDED(hResult))
    {
        pControl->put_Pos(sizeTemp.cx, sizeTemp.cy);
    }

    // SIZE
    hResult = pNode->GetValue(_T("size"), &sizeTemp);
    if(SUCCEEDED(hResult))
    {
        pControl->SetAutoSize(FALSE);
        pControl->put_Size(sizeTemp.cx, sizeTemp.cy);
    }

    // AutoSize
    hResult = pNode->GetValue(_T("autosize"), &bTemp);
    if(SUCCEEDED(hResult))
    {
        pControl->SetAutoSize(bTemp);
    }

    // Id
    bstrTemp = _T("");
    hResult = pNode->GetValue(_T("id"), &bstrTemp);
    if(SUCCEEDED(hResult))
    {
        pControl->put_ItemId(bstrTemp);
    }

    // Text
    hResult = pNode->GetValue(_T("text"), &bstrTemp);
    if(SUCCEEDED(hResult))
    {
        XString strTemp(bstrTemp);
//         if(strTemp.size() > 0 && strTemp.at(0) == _T('@'))
//             strTemp = Util::LoadString(strTemp);
        pControl->put_Text(strTemp.GetData());
    }

    // MINSIZE
    hResult = pNode->GetValue(_T("minsize"), &sizeTemp);
    if(SUCCEEDED(hResult))
        pControl->put_MinSize(sizeTemp.cx, sizeTemp.cy);

    // MAXSIZE
    hResult = pNode->GetValue(_T("maxsize"), &sizeTemp);
    if(SUCCEEDED(hResult))
        pControl->put_MaxSize(sizeTemp.cx, sizeTemp.cy);

    // ZORDER
    IXControl* pParent = NULL;
    hResult = pControl->get_Parent(&pParent);
    if(SUCCEEDED(hResult) && pParent != NULL)
    {
        hResult = pNode->GetValue(_T("zorder"), &bstrTemp);
        if(SUCCEEDED(hResult))
        {
            if(bstrTemp == _T("topmost"))
                pParent->put_ChildZOrder(pControl, ESkin_TopMost);
            else if(bstrTemp == _T("bottommost"))
                pParent->put_ChildZOrder(pControl, ESkin_BottomMost);
            else
                pParent->put_ChildZOrder(pControl, _ttoi(bstrTemp));
        }
    }

    // VISIBLE
    hResult = pNode->GetValue(_T("visible"), &bTemp);
    if(SUCCEEDED(hResult))
        pControl->SetVisible(bTemp);

    // ENABLE
    hResult = pNode->GetValue(_T("enable"), &bTemp);
    if(SUCCEEDED(hResult))
        pControl->SetEnable(bTemp);

    // Flags
    hResult = pNode->GetValue(_T("flags"), &bstrTemp);
    if(SUCCEEDED(hResult))
    {
        size_t handle = 0;
        XString strToken;
        XString strFlags = bstrTemp;
        while(StrUtil::NextToken(strFlags, handle, _T(";"), strToken, FALSE))
        {
            if(strToken == _T("track"))
                pControl->ModifyControlFlags(ESkin_Track, TRUE);
            else if(strToken == _T("bottommost"))
                pControl->ModifyControlFlags(ESkin_BottomMost, TRUE);
            else if(strToken == _T("drop"))
                pControl->ModifyControlFlags(ESkin_Drop, TRUE);
            else if(strToken == _T("interactive"))
                pControl->ModifyControlFlags(ESkin_Interactive, TRUE);
        }
    }

    // Margin
    hResult = pNode->GetValue(_T("margin"), &rcWnd);
    if(SUCCEEDED(hResult))
        pControl->put_Margin(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom);

    // Padding
    hResult = pNode->GetValue(_T("padding"), &rcWnd);
    if(SUCCEEDED(hResult))
        pControl->put_Padding(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom);

    // Background
    hResult = pNode->GetValue(_T("background"), &bstrTemp);
    if(SUCCEEDED(hResult))
    {
        IXDraw* pBkg = ParseHelper::ParseBackground(bstrTemp);
        if(pBkg != NULL)
        {
            pControl->put_Background(pBkg);
            pBkg->Release();
        }
    }

    // UserData
    hResult = pNode->GetValue(_T("userdata"), &lTemp);
    if(SUCCEEDED(hResult))
        pControl->put_UserData((DWORD)lTemp);

    return TRUE;
}
